<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户id" prop="merchantId">
        <el-input
          v-model="queryParams.merchantId"
          placeholder="请输入商户id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票抬头" prop="header">
        <el-input
          v-model="queryParams.header"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业税号" prop="taxNumber">
        <el-input
          v-model="queryParams.taxNumber"
          placeholder="请输入企业税号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="mail">
        <el-input
          v-model="queryParams.mail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入企业电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户银行"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankNumber">
        <el-input
          v-model="queryParams.bankNumber"
          placeholder="请输入银行账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入企业地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专票收件姓名" prop="ticketName">
        <el-input
          v-model="queryParams.ticketName"
          placeholder="请输入专票收件姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专票收件电话" prop="ticketPhone">
        <el-input
          v-model="queryParams.ticketPhone"
          placeholder="请输入专票收件电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专票收件地址" prop="ticketAddress">
        <el-input
          v-model="queryParams.ticketAddress"
          placeholder="请输入专票收件地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者id" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建者id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改者id" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入修改者id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:invoice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:invoice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:invoice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:invoice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="商户id" align="center" prop="merchantId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="发票抬头" align="center" prop="header" />
      <el-table-column label="企业税号" align="center" prop="taxNumber" />
      <el-table-column label="发票抬头类型" align="center" prop="headerType" />
      <el-table-column label="买家类型" align="center" prop="buyerType" />
      <el-table-column label="邮箱" align="center" prop="mail" />
      <el-table-column label="企业电话" align="center" prop="phone" />
      <el-table-column label="开户银行" align="center" prop="bankName" />
      <el-table-column label="银行账号" align="center" prop="bankNumber" />
      <el-table-column label="企业地址" align="center" prop="address" />
      <el-table-column label="专票收件姓名" align="center" prop="ticketName" />
      <el-table-column label="专票收件电话" align="center" prop="ticketPhone" />
      <el-table-column label="专票收件地址" align="center" prop="ticketAddress" />
      <el-table-column label="创建者id" align="center" prop="createUser" />
      <el-table-column label="修改者id" align="center" prop="updateUser" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:invoice:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:invoice:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户信息-发票对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="invoiceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户id" prop="merchantId">
          <el-input v-model="form.merchantId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="发票抬头" prop="header">
          <el-input v-model="form.header" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="企业税号" prop="taxNumber">
          <el-input v-model="form.taxNumber" placeholder="请输入企业税号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="企业电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入企业电话" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankNumber">
          <el-input v-model="form.bankNumber" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="企业地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入企业地址" />
        </el-form-item>
        <el-form-item label="专票收件姓名" prop="ticketName">
          <el-input v-model="form.ticketName" placeholder="请输入专票收件姓名" />
        </el-form-item>
        <el-form-item label="专票收件电话" prop="ticketPhone">
          <el-input v-model="form.ticketPhone" placeholder="请输入专票收件电话" />
        </el-form-item>
        <el-form-item label="专票收件地址" prop="ticketAddress">
          <el-input v-model="form.ticketAddress" placeholder="请输入专票收件地址" />
        </el-form-item>
        <el-form-item label="删除标识字段(0-未删除 1-已删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识字段(0-未删除 1-已删除)" />
        </el-form-item>
        <el-form-item label="创建者id" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入创建者id" />
        </el-form-item>
        <el-form-item label="修改者id" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入修改者id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Invoice">
import { listInvoice, getInvoice, delInvoice, addInvoice, updateInvoice } from "@/api/business/invoice";

const { proxy } = getCurrentInstance();

const invoiceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    merchantId: null,
    userId: null,
    header: null,
    taxNumber: null,
    headerType: null,
    buyerType: null,
    mail: null,
    phone: null,
    bankName: null,
    bankNumber: null,
    address: null,
    ticketName: null,
    ticketPhone: null,
    ticketAddress: null,
    createUser: null,
    updateUser: null
  },
  rules: {
    createUser: [
      { required: true, message: "创建者id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商户信息-发票列表 */
function getList() {
  loading.value = true;
  listInvoice(queryParams.value).then(response => {
    invoiceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    merchantId: null,
    userId: null,
    header: null,
    taxNumber: null,
    headerType: null,
    buyerType: null,
    mail: null,
    phone: null,
    bankName: null,
    bankNumber: null,
    address: null,
    ticketName: null,
    ticketPhone: null,
    ticketAddress: null,
    delFlag: null,
    createTime: null,
    createUser: null,
    updateTime: null,
    updateUser: null
  };
  proxy.resetForm("invoiceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商户信息-发票";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInvoice(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商户信息-发票";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["invoiceRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInvoice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInvoice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除商户信息-发票编号为"' + _ids + '"的数据项？').then(function() {
    return delInvoice(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/invoice/export', {
    ...queryParams.value
  }, `invoice_${new Date().getTime()}.xlsx`)
}

getList();
</script>
